<script setup>
import { ref, onMounted, computed } from 'vue';
import LeftPanel from './components/LeftPanel.vue';
import RightPanel from './components/RightPanel.vue';
import TrendingPage from './components/TrendingPage.vue';
import CommentPage from './components/CommentPage.vue';
import TopicArticlePage from './components/TopicArticlePage.vue';
import PyqPage from './components/PyqPage.vue';
import PageSelector from './components/PageSelector.vue';
import LoginPage from './components/LoginPage.vue';
import { isLoggedIn, getUserInfo } from './api/authAPI.js';

const currentPage = ref('article');
const isAuthenticated = ref(false);
const userInfo = ref(null);

// 检查认证状态
const checkAuthStatus = () => {
  isAuthenticated.value = isLoggedIn();
  if (isAuthenticated.value) {
    userInfo.value = getUserInfo();
  }
};

// 处理登录成功
const handleLoginSuccess = () => {
  checkAuthStatus();
  // 跳转到主页面
  window.location.hash = '';
};

// 处理登出
const handleLogout = () => {
  isAuthenticated.value = false;
  userInfo.value = null;
  window.location.hash = '#/login';
};

// 页面初始化时检查认证状态
onMounted(() => {
  checkAuthStatus();
});

const handlePageChange = (page) => {
  currentPage.value = page;
};

const getCurrentComponent = () => {
  switch (currentPage.value) {
    case 'article':
      return RightPanel;
    case 'trending':
      return TrendingPage;
    case 'comment':
      return CommentPage;
    case 'topic':
      return TopicArticlePage;
    case 'pyq':
      return PyqPage;
    default:
      return RightPanel;
  }
};

// 当前路由状态
const currentRoute = ref('main');

// 监听hash变化
const handleHashChange = () => {
  const hash = window.location.hash;
  if (hash === '#/login') {
    currentRoute.value = 'login';
  } else {
    // 如果未登录，强制跳转到登录页面
    if (!isAuthenticated.value) {
      window.location.hash = '#/login';
      return;
    }
    currentRoute.value = 'main';
  }
};

// 页面初始化时检查认证状态
onMounted(() => {
  checkAuthStatus();
  // 延迟处理路由，确保认证状态已更新
  setTimeout(() => {
    handleHashChange();
  }, 100);
  window.addEventListener('hashchange', handleHashChange);
});
</script>

<template>
  <!-- 登录页面 -->
  <LoginPage 
    v-if="currentRoute === 'login'" 
    @login-success="handleLoginSuccess" 
  />
  
  <!-- 主应用页面 -->
  <div v-else-if="isAuthenticated" class="app-wrapper">
    <!-- 顶部导航栏 -->
    <header class="app-header">
      <div class="header-content">
        <h1 class="app-title">媒体内容管理系统</h1>
        <div class="user-info">
          <span class="welcome-text">欢迎，{{ userInfo?.name || userInfo?.username || '用户' }}</span>
          <button @click="handleLogout" class="logout-btn">退出登录</button>
        </div>
      </div>
    </header>
    
    <PageSelector :current-page="currentPage" @page-change="handlePageChange" />
    <main class="app-container">
      <LeftPanel v-if="currentPage === 'article'" />
      <component :is="getCurrentComponent()" />
    </main>
  </div>
  
  <!-- 未认证状态 -->
  <div v-else class="loading-container">
    <div class="loading-spinner"></div>
    <p>正在检查登录状态...</p>
  </div>
</template>

<style scoped>
.app-wrapper {
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
  box-sizing: border-box;
}

.app-header {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  padding: 0 20px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  max-width: clamp(1200px, 90vw, 1800px);
  margin: 0 auto;
}

.app-title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.welcome-text {
  color: #666;
  font-size: 14px;
}

.logout-btn {
  background: #e74c3c;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.logout-btn:hover {
  background: #c0392b;
}

.app-container {
  width: clamp(1200px, 90vw, 1800px);
  min-height: 90vh;
  height: auto;
  display: flex;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.07);
  background-color: var(--panel-bg-color);
  margin: 0 auto;
  overflow: visible;
}

.loading-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  gap: 16px;
}

.loading-container .loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #4a90e2;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.loading-container p {
  color: #666;
  font-size: 16px;
  margin: 0;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>
